package com.rk.pethome.org.controller;

import com.rk.pethome.org.domain.Department;
import com.rk.pethome.org.query.DepartmentQuery;
import com.rk.pethome.org.service.IDepartmentService;
import com.rk.pethome.basic.util.AjaxResult;
import com.rk.pethome.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RequestMapping("/department")
@RestController
public class DepartmentController {

    @Autowired
    IDepartmentService iDepartmentService;


    /**
     * 保存/修改部门
     * @param department
     * @return
     */
    @PostMapping
    public AjaxResult save(@RequestBody Department department) {
        System.out.println(department);
        try {
            iDepartmentService.addOrUpdate(department);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }
    }

    /**
     * 修改部门
     * @param department
     * @return
     */
    @PutMapping
    public AjaxResult update(@RequestBody Department department) {
        try {
            iDepartmentService.update(department);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }
    }


    /**
     *批量删除数据
     * 部门集合
     * @return
     */
    @PatchMapping("{id}")
    public AjaxResult batchRemove(@RequestBody List<Department> departments) {
        try {
            iDepartmentService.batchRemove(departments);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }
    }

    /**
     *根据id删除对象
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public AjaxResult delete(@PathVariable("id")Long id) {
        try {
            iDepartmentService.delete(id );
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }
    }

    /**
     *根据id查询对象
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public Department loadById(@PathVariable("id")Long id)
    {
       return iDepartmentService.loadById(id);
    }



    /**
     *查询所有对象
     * @param
     * @return
     */
    @PatchMapping
    public PageList<Department> loadPageQuery(@RequestBody DepartmentQuery query) {
        System.out.println(query);
        return iDepartmentService.loadPagebyQuery(query);
    }

    /**
     *查询所有的部门列表
     * @return
     */
    @PatchMapping("/loadAll")
    public List<Department>  loadAll(){
        return iDepartmentService.loadAll();
    }

}
